package ar.edu.utn.frgp.labtec.dao;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import ar.edu.utn.frgp.labtec.constants.DBParametersConstants;
import ar.edu.utn.frgp.labtec.entities.EstadoEnum;
import ar.edu.utn.frgp.labtec.entities.RevisionContrato;

public class ContratoDao extends DefaultGenericDao implements GenericDao<RevisionContrato> {


	public void create(RevisionContrato obj) {

		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		obj.setEstado(EstadoEnum.H);
		session.save(obj);
	}
	

	public void delete(RevisionContrato obj) {
	}
	
	@SuppressWarnings("unchecked")
	public List<RevisionContrato> getAll() {
		
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		List<RevisionContrato> resultList = (List<RevisionContrato>)session.createCriteria(RevisionContrato.class).list();
		
		return resultList;
	}


	public void update(RevisionContrato obj) {
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		session.update(obj);
	}
	
	public RevisionContrato getContrato(Integer idContrato, String anioContrato){
		
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		
		Criteria criteria = session.createCriteria(RevisionContrato.class);
		criteria.add(Restrictions.eq(DBParametersConstants.ID_CONTRATO, idContrato));
		criteria.add(Restrictions.eq(DBParametersConstants.ANIO_CONTRATO, anioContrato));
		
		RevisionContrato contrato = (RevisionContrato) criteria.uniqueResult();
		
		return contrato;
		
	}
	
	public RevisionContrato getContrato(Integer idOrden){
		
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		
		Criteria criteria = session.createCriteria(RevisionContrato.class);
		criteria.add(Restrictions.eq(DBParametersConstants.ID_ORDEN, idOrden));
		
		RevisionContrato contrato = (RevisionContrato) criteria.uniqueResult();
		
		return contrato;
		
	}
	
	public Integer getNumeroContrato(String anioContrato){
		Integer result = null;
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		
		Criteria criteria = session.createCriteria(RevisionContrato.class);
		criteria.add(Restrictions.eq(DBParametersConstants.ANIO_CONTRATO, anioContrato));
		criteria.setProjection(Projections.max(DBParametersConstants.ID_CONTRATO));
		
		result = (Integer)criteria.uniqueResult();
		
		if(result == null)
			result = 0;
		
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<RevisionContrato> getContratos(HashMap<String, Object> parameters){
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
		
		List<RevisionContrato> resultList = null;
		
		Criteria criteria = session.createCriteria(RevisionContrato.class);
		
		for(String key : parameters.keySet()){
			criteria.add(Restrictions.eq(key, parameters.get(key)));
		}
		try {
			resultList = (List<RevisionContrato>)criteria.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return resultList;
	}
	

}
